<?php

function amankan($str) {	
	/* versi 1 (disarankan) */
	$retval = mysql_real_escape_string($str);
	/* end of versi 1 (disarankan) */
	
	/* versi 2 (tested) */
	// $retval = str_replace("\\","\\\\",$str);
	// $retval = str_replace('"','\\"',$retval);
	// $retval = str_replace("'","\\'",$retval);
	// $retval = str_replace("\r","\\r",$retval);
	// $retval = str_replace("\n","\\n",$retval);
	// $retval = str_replace("\x00","\\0",$retval);
	// $retval = str_replace("\x1a","\\Z",$retval);
	/* end of versi 2 (tested) */
	
	/* versi 3 (tidak di-escape. tidak disarankan kecuali kalo settingan php-nya udah pake magic quotes maka versi inilah yang paling disarankan) */
	// $retval = $str;
	/* end of versi 3 (tested) */
	
	return $retval;
}

function open_db() {
	$host = "localhost";
	$user = "sotoy";
	$pass = "station";
	$dbname = "cn";
	$con = mysql_connect($host,$user,$pass,true) or die(mysql_error());
	mysql_select_db($dbname,$con) or die(mysql_error());
	return $con;
}

function close_db(&$con) {
	mysql_close($con);
}

function db_result($query) {
	$retval = false;
	$result = mysql_query($query);
	if ($result && (mysql_num_rows($result) > 0)) {
		while ($row = mysql_fetch_assoc($result)) {
			$retval = $row;
			break;
		}
	}
	mysql_free_result($result);
	return $retval;
}

function db_array($query) {
	$retval = array();
	$result = mysql_query($query);
	if ($result && (mysql_num_rows($result) > 0)) {
		while ($row = mysql_fetch_assoc($result)) {
			$retval[] = $row;
		}
	}
	mysql_free_result($result);
	return $retval;
}

function db_insert($query) {
	$result = mysql_query($query);
	if ($result == false) {
		$retval = false;
	} else {
		$retval = mysql_insert_id();
	}
	return $retval;
}

function db_query($query) {
	$retval = mysql_query($query);
	return $retval;
}

function println($string = "") {
	echo $string."\n";
}

function show_status($string) {
	$chunks = explode(' ',$string);
	foreach ($chunks as $key => $chunk) {
		if (strlen($chunk) > 0) {
			if ($chunk[0] == '{') {
				$tokens = explode('|',substr($chunk,1,-1));
				if ($tokens[0] == '@') {
					$chunks[$key] = '<a href="'.make_url('profil','i='.$tokens[2]).'">@'.$tokens[1].'</a>';
				} elseif ($tokens[0] == '#') {
					$chunks[$key] = '<a href="'.make_url('search-result','q=%23'.$tokens[1]).'">#'.$tokens[1].'</a>';
				} elseif ($tokens[0] == 'l') {
					$chunks[$key] = '<a href="http://'.$tokens[1].'">'.$tokens[1].'</a>';
				}
			}
		}
	}
	$retval = implode(' ',$chunks);
	$retval = str_replace('&o;','{',$retval);
	$retval = str_replace('&c;','}',$retval);
	return $retval;
}

function check_session() {
	$retval['is_session_valid'] = false;
	$retval['user'] = false;
	if (isset($_SESSION['cn_id_user'])) {
		$retval['user'] = db_result('SELECT * FROM users WHERE user_id = '.intval($_SESSION['cn_id_user']));
		if ($retval['user'] === false) {
			unset($_SESSION['cn_id_user']);
		} else {
			$retval['is_session_valid'] = true;
		}
	}
	return $retval;
}

function is_mobile() {
	return preg_match("/(opera mini|android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", strtolower($_SERVER["HTTP_USER_AGENT"]));
}

function make_url($page,$other) {
	$retval = '';
	if (isset($_GET['v'])) {
		$retval .= '?v=m';
		if ($page != '') {
			$retval .= '&p='.$page;
		}
		if ($other != '') {
			$retval .= '&'.$other;
		}
	} elseif ($page != '') {
		$retval .= '?p='.$page;
		if ($other != '') {
			$retval .= '&'.$other;
		}
	} elseif ($other != '') {
		$retval .= '?'.$other;
	}
	return $retval;
}

function get_posts($stat_user) {
	$retval['list'] = db_array('SELECT stat_id FROM status WHERE stat_user = '.$stat_user);
	$retval['count'] = count($retval['list']);
	return $retval;
}

function get_followings($user_by) {
	$retval['list'] = db_array('SELECT user_who FROM follow INNER JOIN users ON user_who = user_id WHERE user_by = '.$user_by);
	$retval['count'] = count($retval['list']);
	return $retval;
}

function get_followers($user_who) {
	$retval['list'] = db_array('SELECT user_by FROM follow INNER JOIN users ON user_by = user_id WHERE user_who = '.$user_who);
	$retval['count'] = count($retval['list']);
	return $retval;
}

function time_ago($time)
{
	$periods = array("detik","menit","jam","hari","minggu","bulan","tahun");
	$lengths = array("60","60","24","7","4.35","12");
	$now = time();
	$difference = $now - strtotime($time);
	for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
		$difference /= $lengths[$j];
	}
	$difference = round($difference);
	return "$difference $periods[$j] yang lalu ";
}

function extend_statuses($statuses) {
	foreach ($statuses as $key => $status) {
		$statuses[$key]['stat_date'] = time_ago($statuses[$key]['stat_date']);
		$statuses[$key]['restat_user_id'] = '0';
		$statuses[$key]['restat_username'] = '0';
		$statuses[$key]['restat_name'] = '0';
		$statuses[$key]['reply_username'] = '.';
		if ($status['restat_from'] != '0') {
			$restat_from = db_result('SELECT user_id,username,name,stat_txt FROM status INNER JOIN users ON stat_user = user_id WHERE stat_id = '.$status['restat_from']);
			$statuses[$key]['restat_user_id'] = $restat_from['user_id'];
			$statuses[$key]['restat_username'] = $restat_from['username'];
			$statuses[$key]['restat_name'] = $restat_from['name'];
			$statuses[$key]['stat_txt'] = $restat_from['stat_txt'];
		} elseif ($status['reply_stat'] != '0') {
			$reply_to = db_result('SELECT username FROM status INNER JOIN users ON stat_user = user_id WHERE stat_id = '.$status['stat_id']);
			$statuses[$key]['reply_username'] = $reply_to['username'];
		}
	}
	return $statuses;
}

function next_status($id) {
	$status = db_array('SELECT * FROM status WHERE stat_id = '.(intval($id)+1));
	foreach ($status as $key => $stat) {
		$stat[$key]['stat_txt'] = 'test for test';
		$stat[$key]['reply_stat'] = 12;
	}
	return $status;
}
